package com.chcit.mobile.helper;

import android.support.annotation.NonNull;

import com.chcit.mobile.app.utils.FileUtil;
import com.chcit.mobile.mvp.model.api.service.UserService;
import com.jess.arms.base.app.Quick;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.utils.ArmsUtils;
import com.vector.update_app.HttpManager;

import java.io.File;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;


/**
 * Created by Vector
 * on 2017/6/19 0019.
 */

public class UpdateAppHttpUtil implements HttpManager {


    /*public UpdateAppHttpUtil(IRepositoryManager repositoryManager){
        mIRepositoryManager = repositoryManager;
    }*/
    /**
     * 异步get
     *
     * @param url      get请求地址
     * @param params   get参数
     * @param callBack 回调
     */
    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {
        ArmsUtils.obtainAppComponentFromContext(Quick.getApplicationContext()).repositoryManager().obtainRetrofitService(UserService.class)
                .update(url)
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String responseBody) {
                        callBack.onResponse(responseBody);
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }


                })
              //  .subscribeOn((Schedulers.io())

              ;


      /*  OkHttpUtils.get()
                .url(url)
                .params(params)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Response response, Exception e, int id) {
                        callBack.onError(validateError(e, response));
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }
                });*/
    }

    /**
     * 异步post
     *
     * @param url      post请求地址
     * @param params   post请求参数
     * @param callBack 回调
     */
    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {


    }

    /**
     * 下载
     *
     * @param url      下载地址
     * @param path     文件保存路径
     * @param fileName 文件名称
     * @param callback 回调
     */
    @Override
    public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) {
        FileDownLoadObserver<File> fileDownLoadObserver = new FileDownLoadObserver<File>() {
            @Override
            public void onDownLoadSuccess(File o) {
                callback.onResponse(o);
            }

            @Override
            public void onDownLoadFail(Throwable throwable) {

            }

            @Override
            public void onProgress(int progress, long total) {
                callback.onProgress(progress,total);
            }
        };
        ArmsUtils.obtainAppComponentFromContext(Quick.getApplicationContext()).repositoryManager().obtainRetrofitService(UserService.class)
                .down(url)
                .observeOn(Schedulers.io())
                .observeOn(Schedulers.computation())//需要
                .map(responseBody -> fileDownLoadObserver.saveFile(responseBody, FileUtil.DOWN_DIR, fileName))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(fileDownLoadObserver);



       /* OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(path, fileName) {
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        callback.onProgress(progress, total);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e, int id) {
                        callback.onError(validateError(e, response));
                    }

                    @Override
                    public void onResponse(File response, int id) {
                        callback.onResponse(response);

                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        callback.onBefore();
                    }
                });*/

    }
}